package com.car.common.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.car.common.model.base.BaseUsers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.utils.JwtToken;
import com.utils.MD5Util;
import com.utils.PicUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;
import com.utils.Toolkit;

@SuppressWarnings("serial")
public class Users extends BaseUsers<Users> {
	public final static Users dao = new Users();
	
	public final static String USER_PASSWORD_KEY = "^&AYSUDVFG*&ASTFASNF*ASYFBBASHJASFPXOZUCF(4658";//用户加密key
	
	public final static String API_TOKEN_KEY = "JBFAFB^^&%^&*&68t78ydasdgby,.p;Asguigf87tasyfvgys";//接口key
	    
	public static String password(String password) {
	    return MD5Util.php(MD5Util.sha1(password) + USER_PASSWORD_KEY);
	}
	
	
	public Map<String,Object> info(Users user, String from){
		Map<String,Object> userTemp = Maps.newHashMap();
		if (user == null) {
			return userTemp;
		}
		/*  ,auth_badge,vip_badpe,consume_badpe,credit
		auth_badge	tinyint	2	0	-1	0	0	0	0	0	0	0  1  手机认证  2 性别 认证   3 实名认证				0	0
		vip_badpe	tinyint	2	0	-1	0	0	0	0	0	0	0  1 白银  2 黄金  3 黑金				0	0
		consume_badpe	tinyint	2	0	-1	0	0	0	0	0	0	0  1 白银  2 黄金  3 黑金				0	0
		credit	smallint	6	0	-1	0	0	0	0	0	0	信用积分 				0	0
		*/
		userTemp.put("is_show_nearby",   user.get("is_show_nearby"));
		userTemp.put("talk_badge",     	 user.get("talk_badge"));
		userTemp.put("auth_badge",     	 user.get("auth_badge"));
		userTemp.put("vip_badpe",     	 user.get("vip_badpe"));
		userTemp.put("consume_badpe",    user.get("consume_badpe"));
		if (user.get("credit") != null && user.getInt("credit") > 10000) {
			userTemp.put("credit_badge", 3);
		} else if (user.get("credit") != null && user.getInt("credit") > 1000) {
			userTemp.put("credit_badge", 2);
		} else if (user.get("credit") != null && user.getInt("credit") > 100) {
			userTemp.put("credit_badge", 1);
		} else {
			userTemp.put("credit_badge", 0);
		}
		
		userTemp.put("vip_end_time",     user.get("vip_end_time_string"));
		int vip = (user.getVipEndTime() < (SystemClock.now()/1000L)) ? 2 : 1;
		userTemp.put("vip",              vip);
		if (vip == 2) {
			userTemp.put("vip_badpe",    0);
		}
		userTemp.put("is_true_name",     user.get("is_true_name") == null ? 1: 0);
		userTemp.put("user_mobile",      user.get("user_mobile") == null ? "18312345678" : user.getStr("user_mobile"));
		userTemp.put("user_id",          user.get("id"));
		userTemp.put("search_id",        user.getId());
		userTemp.put("birthday",         user.get("birthday_string"));
		userTemp.put("age",              Toolkit.one.age(user.getBirthday()));
		{
			Map<String,Object> sexTemp = Maps.newHashMap();
			sexTemp.put("code", user.getSex());
			sexTemp.put("name", user.get("sex_name"));
			userTemp.put("sex", sexTemp);
		}
		userTemp.put("nick_name",        StringUtil.decode(user.getNickName()));
		if (StringUtil.empty(user.getUserHeader())) {
			userTemp.put("user_header",      PicUtil.DEFAULT_HEAD );
		} else {
			// + PicUtil.QINIU_CUT_ADMIN
			userTemp.put("user_header",      PicUtil.QINIU_DIR + user.getUserHeader() );
		}
		
		userTemp.put("area_name",        user.get("area_name"));
		if (StringUtil.empty(user.getUserBg())) {
			userTemp.put("user_bg",     	 PicUtil.DEFAULT_USER_BG );
		} else {
			//  + PicUtil.QINIU_THIN
			userTemp.put("user_bg",          PicUtil.QINIU_DIR + user.get("user_bg"));
		}
		
		
		userTemp.put("show_money_out",   2);
		userTemp.put("update_time",      user.get("update_time"));
		// user_tags
		String  user_tags = user.getTagIds();
		if (!StringUtil.empty(user_tags)) {
			String[] tags = user_tags.split(",");
			if (tags == null || tags.length == 0) {
				userTemp.put("user_tags_num", 0);
				userTemp.put("user_tags",  Lists.newArrayList());
			} else {
				userTemp.put("user_tags_num", tags.length);
				ArrayList<Tags> tagsList = CacheKit.get("constant", "tagsList", new IDataLoader(){
				       public Object load() {
				           return Tags.dao.getAll();
				}});
				List<Map<String,Object>> user_tags_list = Lists.newArrayList();
				int size = tags.length;
				if ("card_list".equals(from)) {
					size = Math.min(3, size);
				}
				for(int i = 0 ; i < size ; i++) {
					String id = tags[i];
					for(Tags tag :tagsList) {
						if(tag != null && !StringUtil.empty(id)) {
							if (tag.getId() == new Long(id).longValue()) {
								Map<String,Object> tagTemp = Maps.newHashMap();
								tagTemp.put("code", tag.getId());
								tagTemp.put("name", tag.getName());
								user_tags_list.add(tagTemp);
								break;
							}
						}
					}
				}
				userTemp.put("user_tags", user_tags_list);
			}
		}
		String  user_private_tags = user.get("private_tag_ids");
		if (!StringUtil.empty(user_private_tags)) {
			String[] tags = user_private_tags.split(",");
			if (tags == null || tags.length == 0) {
				userTemp.put("user_tags_num", 0);
				userTemp.put("user_tags",  Lists.newArrayList());
			} else {
				userTemp.put("user_tags_num", tags.length);
				
				ArrayList<Tags> tagsList = CacheKit.get("constant", "privateTagsList", new IDataLoader(){
				       public Object load() {
				           return Tags.dao.getPrv();
				}});
				List<Map<String,Object>> user_tags_list = Lists.newArrayList();
				for(int i = 0 ; i < tags.length ; i++) {
					String id = tags[i];
					for(Tags tag :tagsList) {
						if(tag != null && !StringUtil.empty(id)) {
							if (tag.getId() == new Long(id).longValue()) {
								Map<String,Object> tagTemp = Maps.newHashMap();
								tagTemp.put("code", tag.getId());
								tagTemp.put("name", tag.getName());
								user_tags_list.add(tagTemp);
								break;
							}
						}
					}
				}
				userTemp.put("user_private_tags", user_tags_list);
			}
		}
		UsersInfo info = UsersInfo.dao.findFirst("select * from users_info where user_id = " + user.getId());
		if(info != null){
			userTemp.put("introduction", StringUtil.decode(info.getIntroduction()));
			{
				Map<String,Object> heightTemp = Maps.newHashMap();
				heightTemp.put("code", info.getHeight());
				heightTemp.put("name", info.getHeight()+"cm");
				userTemp.put("height", heightTemp);
			}
			{
				Map<String,Object> weightTemp = Maps.newHashMap();
				weightTemp.put("code", info.getWeight());
				weightTemp.put("name", info.getWeight()+"kg");
				userTemp.put("height", weightTemp);
			}
			{
				Map<String,Object> weightTemp = Maps.newHashMap();
				weightTemp.put("code", info.getWeight());
				weightTemp.put("name", info.getWeight()+"kg");
				userTemp.put("height", weightTemp);
			}
			{
				Map<String,Object> orientationTemp = Maps.newHashMap();
				orientationTemp.put("code", info.getOrientation());
				orientationTemp.put("name", orientation_arr.get(info.getOrientation()));
				userTemp.put("orientation", orientationTemp);
			}
			{
				Map<String,Object> emotionTemp = Maps.newHashMap();
				emotionTemp.put("code", info.getEmotion());
				emotionTemp.put("name", orientation_emotion.get(info.getEmotion()));
				userTemp.put("emotion", emotionTemp);
			}
		}
		
		return userTemp;
	}
	
	
	public Map<String,Object> info2(Users user){
		Map<String,Object> userTemp = Maps.newHashMap();
		if (user == null) {
			return userTemp;
		}
		/*  ,auth_badge,vip_badpe,consume_badpe,credit
		auth_badge	tinyint	2	0	-1	0	0	0	0	0	0	0  1  手机认证  2 性别 认证   3 实名认证				0	0
		vip_badpe	tinyint	2	0	-1	0	0	0	0	0	0	0  1 白银  2 黄金  3 黑金				0	0
		consume_badpe	tinyint	2	0	-1	0	0	0	0	0	0	0  1 白银  2 黄金  3 黑金				0	0
		credit	smallint	6	0	-1	0	0	0	0	0	0	信用积分 				0	0
		*/
		userTemp.put("is_show_nearby",   user.get("is_show_nearby"));
		userTemp.put("talk_badge",     	 user.get("talk_badge"));
		userTemp.put("auth_badge",     	 user.get("auth_badge"));
		userTemp.put("vip_badpe",     	 user.get("vip_badpe"));
		userTemp.put("consume_badpe",    user.get("consume_badpe"));
		if (user.get("credit") != null && user.getInt("credit") > 10000) {
			userTemp.put("credit_badge", 3);
		} else if (user.get("credit") != null && user.getInt("credit") > 1000) {
			userTemp.put("credit_badge", 2);
		} else if (user.get("credit") != null && user.getInt("credit") > 100) {
			userTemp.put("credit_badge", 1);
		} else {
			userTemp.put("credit_badge", 0);
		}
		
		
		userTemp.put("vip_end_time",     user.get("vip_end_time_string"));
		int vip = (user.getVipEndTime() < (SystemClock.now()/1000L)) ? 2 : 1;
		userTemp.put("vip",              vip);
		if (vip == 2) {
			userTemp.put("vip_badpe",    0);
		}
		userTemp.put("is_true_name",     user.get("is_true_name") == null ? 1: 0);
		userTemp.put("user_mobile",      user.get("user_mobile") );
		userTemp.put("user_id",          user.get("id"));
		userTemp.put("search_id",        user.getId());
		userTemp.put("birthday",         user.get("birthday_string"));
		userTemp.put("age",              Toolkit.one.age(user.getBirthday()));
		{
			Map<String,Object> sexTemp = Maps.newHashMap();
			sexTemp.put("code", user.getSex());
			sexTemp.put("name", user.get("sex_name"));
			userTemp.put("sex", sexTemp);
		}
		userTemp.put("nick_name",        StringUtil.decode(user.getNickName()));
		if (StringUtil.empty(user.getUserHeader())) {
			userTemp.put("user_header",      PicUtil.DEFAULT_HEAD );
		} else {
			// + PicUtil.QINIU_CUT_ADMIN
			userTemp.put("user_header",      PicUtil.QINIU_DIR + user.getUserHeader() );
		}
		userTemp.put("area_id",        user.get("area_id"));
		userTemp.put("area_name",        user.get("area_name"));
		if (StringUtil.empty(user.getUserBg())) {
			userTemp.put("user_bg",     	 PicUtil.DEFAULT_USER_BG );
		} else {
			//  + PicUtil.QINIU_THIN
			userTemp.put("user_bg",          PicUtil.QINIU_DIR + user.get("user_bg"));
		}
		
		
		userTemp.put("show_money_out",   2);
		userTemp.put("update_time",      user.get("update_time"));
		// user_tags
		String  user_tags = user.getTagIds();
		if (!StringUtil.empty(user_tags)) {
			String[] tags = user_tags.split(",");
			if (tags == null || tags.length == 0) {
				userTemp.put("user_tags_num", 0);
				userTemp.put("user_tags",  Lists.newArrayList());
			} else {
				userTemp.put("user_tags_num", tags.length);
				ArrayList<Tags> tagsList = CacheKit.get("constant", "tagsList", new IDataLoader(){
				       public Object load() {
				           return Tags.dao.getAll();
				}});
				List<Map<String,Object>> user_tags_list = Lists.newArrayList();
				for(int i = 0 ; i < tags.length ; i++) {
					String id = tags[i];
					for(Tags tag :tagsList) {
						if(tag != null && !StringUtil.empty(id)) {
							if (tag.getId() == new Long(id).longValue()) {
								Map<String,Object> tagTemp = Maps.newHashMap();
								tagTemp.put("code", tag.getId());
								tagTemp.put("name", tag.getName());
								user_tags_list.add(tagTemp);
								break;
							}
						}
					}
				}
				userTemp.put("user_tags", user_tags_list);
			}
		}
		String  user_private_tags = user.get("private_tag_ids");
		if (!StringUtil.empty(user_private_tags)) {
			String[] tags = user_private_tags.split(",");
			if (tags == null || tags.length == 0) {
				userTemp.put("user_tags_num", 0);
				userTemp.put("user_tags",  Lists.newArrayList());
			} else {
				userTemp.put("user_tags_num", tags.length);
				
				ArrayList<Tags> tagsList = CacheKit.get("constant", "privateTagsList", new IDataLoader(){
				       public Object load() {
				           return Tags.dao.getPrv();
				}});
				List<Map<String,Object>> user_tags_list = Lists.newArrayList();
				for(int i = 0 ; i < tags.length ; i++) {
					String id = tags[i];
					for(Tags tag :tagsList) {
						if(tag != null && !StringUtil.empty(id)) {
							if (tag.getId() == new Long(id).longValue()) {
								Map<String,Object> tagTemp = Maps.newHashMap();
								tagTemp.put("code", tag.getId());
								tagTemp.put("name", tag.getName());
								user_tags_list.add(tagTemp);
								break;
							}
						}
					}
				}
				userTemp.put("user_private_tags", user_tags_list);
			}
		}
		UsersInfo info = UsersInfo.dao.findFirst("select * from users_info where user_id = " + user.getId());
		if(info != null){
			userTemp.put("introduction", StringUtil.decode(info.getIntroduction()));
			{
				Map<String,Object> heightTemp = Maps.newHashMap();
				heightTemp.put("code", info.getHeight());
				heightTemp.put("name", info.getHeight()+"cm");
				userTemp.put("height", heightTemp);
			}
			{
				Map<String,Object> weightTemp = Maps.newHashMap();
				weightTemp.put("code", info.getWeight());
				weightTemp.put("name", info.getWeight()+"kg");
				userTemp.put("height", weightTemp);
			}
			{
				Map<String,Object> weightTemp = Maps.newHashMap();
				weightTemp.put("code", info.getWeight());
				weightTemp.put("name", info.getWeight()+"kg");
				userTemp.put("height", weightTemp);
			}
			{
				Map<String,Object> orientationTemp = Maps.newHashMap();
				orientationTemp.put("code", info.getOrientation());
				orientationTemp.put("name", orientation_arr.get(info.getOrientation()));
				userTemp.put("orientation", orientationTemp);
			}
			{
				Map<String,Object> emotionTemp = Maps.newHashMap();
				emotionTemp.put("code", info.getEmotion());
				emotionTemp.put("name", orientation_emotion.get(info.getEmotion()));
				userTemp.put("emotion", emotionTemp);
			}
		}
		
		return userTemp;
	}
	
	public static Map<Integer,String> orientation_arr = Maps.newHashMap();
	
	public static Map<Integer,String> orientation_emotion = Maps.newHashMap();
	
	static {
		orientation_arr.put(1, "男");
		orientation_arr.put(2, "女");
		orientation_arr.put(3, "双向");
		
		
		orientation_emotion.put(1, "已婚");
		orientation_emotion.put(2, "单身");
		orientation_emotion.put(3, "恋爱中");
		
	}
	
	public void embrace(int user_id,int to_user,int money){
		int half = (int)(money/2);
		Db.update("update users set hot_val = hot_val -" + money + " where id= "+ user_id);
		Db.update("update users set free_hot = free_hot +" + half  + " where id= "+ to_user);
		
	}
	
	public static void main(String[] args) {
		//d7e29434083fb90b1b7b9e63c2c3b533
		//System.out.println(Users.password("148"));
		try {
			System.out.println(JwtToken.createToken(148L));
			String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODY1NzU4OTQsImlzcyI6IlNlcnZpY2UiLCJhdWQiOiJBUFAiLCJ1c2VyX2lkIjoiMTc1IiwiaWF0IjoxNTU1MDM5ODk0fQ.hgPluT7-p5KlI4vIzG2cUy5T3GZUTwesdDNmwEmlllc";
			System.out.println(JwtToken.getAppUID(token)); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
